package com.jiutong.meipenggang.utils;

import android.content.Context;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.widget.EditText;
import android.widget.TextView;

import com.jiutong.meipenggang.R;

/**
 * 作用:验证码倒计时封装工具
 * Created by Veken on 2017/7/3.
 */

public class TimerUtil {

    private  EditText et;
    private CountDownTimer timer;
    private TextView tv;
    private long time;
    private int color = Color.parseColor("#ffffff");


    public TimerUtil(TextView tv) {
        this.tv = tv;
    }

    public TimerUtil(TextView tv,long time) {
        this.tv = tv;
        this.time = time;
    }

    public TimerUtil(TextView tv,long time,int color) {
        this.tv = tv;
        this.time = time;
        this.color = color;
    }

    public TimerUtil(TextView tv, EditText et) {
        this.tv = tv;
        this.et = et;
    }
    //设置倒计时
    public void setCountTimer(){
        timer = new CountDownTimer(60000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                tv.setBackgroundResource(R.drawable.shape_verify_code_bg);
                tv.setClickable(false);
                tv.setText(millisUntilFinished / 1000 + "′");
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                tv.setBackgroundResource(R.drawable.shape_verify_code_blue_bg);
                tv.setClickable(true);
                tv.setText("重新发送");
            }
        };
    }

    //设置倒计时
    public void setCountTimerEt(){
        timer = new CountDownTimer(60000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                tv.setTextColor(Color.parseColor("#ffffff"));
                tv.setText(millisUntilFinished / 1000 + "′");
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                tv.setTextColor(Color.parseColor("#ffffff"));
                tv.setText("重新发送");
                et.setText("");
            }
        };
    }

    public void startTimer(){
        timer.start();
    }
}
